<?php

namespace app\service;

use app\controller\SystemUser;
use app\model\OrderOtherCertificateBusiness;
use app\model\SystemBusiness;

/**
 * 订单管理-证书办理
 */
class OrderCertTransact
{
    public function spliceWhere($params = [])
    {
        $model = new OrderOtherCertificateBusiness();
        $c_ids = $model->getIds();
        //证书编号
        if (isset($params['certificate_code']) && $params['certificate_code']) {
            $list = $model
                ->where('certificate_code', 'like', '%' . $params['certificate_code'] . '%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //创建时间
        if ((isset($params['create_start_date']) && $params['create_start_date']) && (isset($params['create_end_date']) && $params['create_end_date'])) {
            $start_time = $params['create_start_date'] . ' 00:00:00';
            $end_time = $params['create_end_date'] . ' 23:59:59';
            $list = $model->where('create_date', 'between', [$start_time, $end_time])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //业务类型
        if (isset($params['business_id']) && $params['business_id']) {
            $businessId = (new SystemBusiness)->whereRaw ('business_name= "'.$params['business_id'].'" OR business_category = "'.$params['business_id'].'"')
                ->column ('id');

            $list = $model
                ->where('business_id','in', $businessId)
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
       
        //销售日期
//        if ((isset($params['sale_start_date']) && $params['sale_start_date']) && (isset($params['sale_end_date']) && $params['sale_end_date'])) {
//            $start_time = $params['sale_start_date'] . ' 00:00:00';
//            $end_time = $params['sale_end_date'] . ' 23:59:59';
//            $list = $model->where('sale_date', 'between', [$start_time, $end_time])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
//            //两个数组的交集并赋值给person_ids
//            $c_ids = $this->handlePersonIds($c_ids, $ids);
//        }
        //人才姓名
        if (isset($params['person_name']) && $params['person_name']) {
            $list = $model
                ->where('person_name', 'like', '%' . $params['person_name'] . '%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //人才身份证号
        if (isset($params['person_id_card']) && $params['person_id_card']) {
            $list = $model
                ->where('person_id_card','like', '%'.$params['person_id_card'].'%')
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书类别
        if (isset($params['certificate_leibie']) && $params['certificate_leibie']) {
            $list = $model
                ->where('certificate_leibie', $params['certificate_leibie'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书类型
        if (isset($params['certificate_leixing']) && $params['certificate_leixing']) {
            $list = $model
                ->where('certificate_leixing', $params['certificate_leixing'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书等级
        if (isset($params['certificate_dengji']) && $params['certificate_dengji']) {
            $list = $model
                ->where('certificate_dengji', $params['certificate_dengji'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书专业
        if (isset($params['certificate_zhuanye']) && $params['certificate_zhuanye']) {
            $list = $model
                ->where('certificate_zhuanye', $params['certificate_zhuanye'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //成本价格
        if (
            (
                (isset($params['cost_start_price']) && $params['cost_start_price'])
                ||
                $params['cost_start_price'] ===0
                ||
                $params['cost_start_price'] === '0'
            )
            &&
            (
                (isset($params['cost_end_price']) && $params['cost_end_price'])
                ||
                $params['cost_end_price'] ===0
                ||
                $params['cost_end_price'] === '0'
            )
        ) {
            $list = $model->where('cost_price', 'between', [$params['cost_start_price'], $params['cost_end_price']])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //实际价格
        if (
            (
                (isset($params['sale_start_price']) && $params['sale_start_price'])
                ||
                $params['sale_start_price'] ===0
                ||
                $params['sale_start_price'] === '0'
            )
            &&
            (
                (isset($params['sale_end_price']) && $params['sale_end_price'])
                ||
                $params['sale_end_price'] ===0
                ||
                $params['sale_end_price'] === '0'
            )
        ) {
            $list = $model->where('sale_price', 'between', [$params['sale_start_price'], $params['sale_end_price']])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //销售部门
        if (isset($params['get_org']) && $params['get_org']) {
            $department_ids = SystemUser::getDepartmentAllUser($params['get_org']);
            $list = $model
                ->where('get_org', 'in', $department_ids)
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //销售人员
        if (isset($params['get_uid']) && $params['get_uid']) {
            $list = \app\model\User::where('nickname', 'like', '%'.$params['get_uid'].'%')
                ->field('id')
                ->select()
                ->toArray();
            $user_ids = array_column($list, 'id');
            $list = $model
                ->where('get_uid', 'in', $user_ids)
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //状态
        if (isset($params['status']) && $params['status']) {
            $list = $model
                ->where('status',  $params['status'])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        return $c_ids;
    }

    public function handlePersonIds($enter_ids, $other_ids)
    {
        if (!$enter_ids || !$other_ids) {
            return [];
        }
        if ($enter_ids && $other_ids) {
            $enter_ids = array_intersect($enter_ids, $other_ids);
        } elseif ($enter_ids && !$other_ids) {
            $enter_ids = [];
        } elseif (!$enter_ids && $other_ids) {
            $enter_ids = array_intersect($enter_ids, $other_ids);
        } elseif (!$enter_ids && !$other_ids) {
            $enter_ids = [];
        }
        return $enter_ids;
    }
}